function addMega(){
	$(this).addClass("hovering");
	$(this).children("a:first").addClass("current");
}

function removeMega(){
	$(this).removeClass("hovering");
	$(this).children("a:first").removeClass("current");

}
//Beautiful Wait function
function pausecomp(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
} 

//isDate
function isDate(dateStr) {

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
	//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
	return false;
	}

	month = matchArray[1]; // p@rse date into variables
	day = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) { // check month range
	//alert("Month must be between 1 and 12.");
	return false;
	}

	if (day < 1 || day > 31) {
	//alert("Day must be between 1 and 31.");
	return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	//alert("Month "+month+" doesn`t have 31 days!")
	return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			//alert("February " + year + " doesn`t have " + day + " days!");
			return false;
		}
	}
	return true; // date is valid
}

// This function removes non-numeric characters
function stripNonNumeric( str )
{
	str += '';
	var rgx = /^\d|\.|-$/;
	var out = '';
	for( var i = 0; i < str.length; i++ )
	{
		if( rgx.test( str.charAt(i) ) ){
			if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) ||
					( str.charAt(i) == '-' && out.length != 0 ) ) ){
				out += str.charAt(i);
			}
		}
	}
	return out;
}

//this function adds commas to string
function addCommas(nStr){
	
	var retStr = "";
	if(jQuery.trim(nStr).length != 0)
	{
		retStr = nStr.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
	}
	return retStr
}

//formats number to specified number of decimals
function format_number(pnumber,decimals){
if (isNaN(pnumber)) { return 0};
if (pnumber=='') { return 0};
var snum = new String(pnumber);
var sec = snum.split('.');
var whole = parseFloat(sec[0]);
var result = '';
if(sec.length > 1){
 var dec = new String(sec[1]);
dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
var dot = dec.indexOf('.');
if(dot == -1){
 dec += '.';
dot = dec.indexOf('.');
}
while(dec.length <= dot + decimals) { dec += '0'; }
result = dec;
} else{
var dot;
var dec = new String(whole);
dec += '.';
dot = dec.indexOf('.');       
while(dec.length <= dot + decimals) { dec += '0'; }
result = dec;
}   
return result;
}

//remove commas from a string
function stripCommas(numString) {
    var re = /,/g;
    return numString.replace(re,"");
}

$(document).ready(function(){
	
	
	//IE Kill it with fire
	if ($.browser.msie) {
	    $("#body_wrapper").addClass("hide");
	    $("#justsaynotoie").removeClass("hide");
	}
	
	$(".formatNumber").live('blur', function(ev) {
		var val = $(this).val();
		val = stripNonNumeric(val);
		val = format_number(val,2);
		//$(this).val(addCommas(val));
		
		if($(this).attr("name") == "tierUpperAmt" && jQuery.trim($(this).val()).length != 0)
		{
			$(this).parent().parent().next().children("td").children("input[name='tierLowerAmt']").val(($(this).val()+1));
			$(this).parent().parent().next().children("td").children("input[name='subtractAmt']").val($(this).val());
		}
		
		val = null;
	});
	
	$('input.formatNumber,input.restrict').live('keypress', function(e) {
		return ( e.which !=13 && e.which!=8 && e.which!=0 && (e.which<46 || e.which == 47 || e.which>57)) ? false : true ;
	});
	
	/* moving the cursor up down left right by arrow keys on tier tables*/
	$("input[type='text'].default.formatNumber").live('keydown', function(e) {
		
		if(e.which == 40)
		{
			var name = $(this).attr("name");
			$(this).parent().parent().next().children("td").children("input[name='"+name+"']").focus();
		}
		
		if(e.which == 37)
		{
			$(this).parent().prev().children("input").focus();
		}
		
		if(e.which == 39)
		{
			$(this).parent().next().children("input").focus();
			
		}
		if(e.which == 38)
		{
			var name = $(this).attr("name");
			$(this).parent().parent().prev().children("td").children("input[name='"+name+"']").focus();
			
		}
	});
	
	
	/*
	 * 
	 * 
	 * Creating Test Cases Common Events
	 * 
	 */
	
	$("a.add_group").live('click', function(ev) {
		var clone = $("div.template:last").clone();
		$(clone).insertAfter($("div.template:last"));
		$("div.template:last").children("div.template_ticker").children("div.mark").children("h3").empty().append($("div.template").size());
		
		//adding droppable where applicable
		$(clone).find(".transactionquestion_drop").droppable({
			 accept: ".draggable.transactionquestion",
			 activeClass: 'droppable-active',
			 over: function(ev,ui)
			 {
				$(this).addClass('droppable-hover');
			 },
		 	 out: function(ev,ui)
		 	 {
				 $(this).removeClass('droppable-hover');
		 	 },
			 drop: function(ev, ui) {
		 		var $question = $(ui.draggable).clone().children(".question").html();// Create a clone of the draggable item and get the id from it
		 		
		 		$(this).append("<div style='margin-left: 10px;' class='input_wrap remove-option'>"
 		 				+"<div class='question_wrap'>"
 		 				+"<label><strong>"+$question+"</strong></label>"
 		 				+"<div class='spacer' style='height: 5px;'><br/></div>"
 		 				+"<input type='text' class='tq'/>"
 		 				+"<input type='hidden' class='question' value='"+$question+"'/>"
 		 				+"</div>"
 		 				+"<a href='#remove' class='remove_question' title='remove' ><img src='/testgin/static/images/delete1.png'  border='0'/></a>"
 		 				+"</div>");
		 		$question = null;
		 		
		 		//<img src='/testgin/static/images/delete1.png'
		 		
			 	$(this).removeClass('droppable-hover');
			 }
		});
		
		
		$(clone).find(".policyquestion_drop").droppable({
			 accept: ".draggable.policyquestion",
			 activeClass: 'droppable-active',
			 over: function(ev,ui)
			 {
				$(this).addClass('droppable-hover');
			 },
		 	 out: function(ev,ui)
		 	 {
				 $(this).removeClass('droppable-hover');
		 	 },
			 drop: function(ev, ui) {
		 		var $question = $(ui.draggable).clone().children(".question").html();// Create a clone of the draggable item and get the id from it
		 		
		 		$(this).append("<div style='margin-left: 10px;' class='input_wrap remove-option'>"
 		 				+"<div class='question_wrap'>"
 		 				+"<label><strong>"+$question+"</strong></label>"
 		 				+"<div class='spacer' style='height: 5px;'><br/></div>"
 		 				+"<input type='text' class='pq'/>"
 		 				+"<input type='hidden' class='question' value='"+$question+"'/>"
 		 				+"</div>"
 		 				+"<a href='#remove' class='remove_question' title='remove' ><img src='/testgin/static/images/delete1.png'   border='0'/></a>"
 		 				+"</div>");
		 		
		 		$question = null;
		 		
			 	$(this).removeClass('droppable-hover');
			 }
		});
		
		
		clone = null;
	});
	$("a.remove_group").live('click', function(ev) {
		if($("div.template").size() != 1)
		{
			$("div.template:last").remove();
		}
	});
	
	
	/*
	 * 
	 * Remove Input Error From Selects on Focus
	 * 
	 */
	

	$("input,select").live('focus', function(ev) {
		$(this).removeClass("inputerror");
	});
	
	
	/*
	 * 
	 *   Bread Crumb Navigation
	 * 
	 * 
	 */
	
	
	$("#group_prev, #crumb_1").live('click', function(ev) {
				
		$("#test_group_block").addClass("hide");
		$("#review_block").addClass("hide");
		$("#create_scenario_block").removeClass("hide");
		$("#crumb_2").children("a:first").removeClass("active");
		$("#crumb_3").children("a:first").removeClass("active");
		$("#crumb_1").children("a:first").addClass("active");
				
	});

	$("#save_prev, #scenario_next,#crumb_2").live('click', function(ev) {

		$("#crumb_1").children("a:first").removeClass("active");
		$("#crumb_3").children("a:first").removeClass("active");
		$("#crumb_2").removeClass("hide").children("a:first").addClass("active");
		$("#create_scenario_block").addClass("hide");
		$("#review_block").addClass("hide");
		$("#test_group_block").removeClass("hide");

	});

	$("#group_next,#crumb_3").live('click', function(ev) {

		$("#crumb_1").children("a:first").removeClass("active");
		$("#crumb_2").children("a:first").removeClass("active");
		$("#crumb_3").removeClass("hide").children("a:first").addClass("active");
		$("#create_scenario_block").addClass("hide");
		$("#test_group_block").addClass("hide");
		$("#review_block").removeClass("hide");

	});
	
	//Sortable Tables
	$(".sortable").tablesorter(); 
	

	/****** Filter by State On Change  ********/
	$("#scenario_state").change(function() {
		var stateId = $("#scenario_state").val();

		if(stateId >= 0)
		{
			$("#filter_by_state_form").submit();
		}
	});
	
	
	
});


